<?php
class db_prefrences
{
	function __construct()
	{
		Global $mySql, $lib;
		$this->mySql = $mySql;
		$this->lib = $lib;
	}
	public function setLoginID($id)
	{
		$this->LoginID = $id;
	}
	public function getPrefrences($id)
	{
		$sql = "SELECT prefs.Home_Opt, prefs.Reply_Opt, prefs.Everyone_Opt, prefs.FFUser, prefs.FFKey
			FROM prefrences prefs
			WHERE User_ID = %d";
		$query = sprintf($sql,
			mysql_real_escape_string($id));
		$result = $this->mySql->makeQuery($query);
		$temp = mysqli_fetch_array($result);
		$this->mySql->listErrors();
		// Feed Prefrences
		if($temp["Home_Opt"] == 1)
		{
			$prefs['display']["Home"] = true;
		}
		else
		{
			$prefs['display']["Home"] = false;
		}
		
		if($temp["Reply_Opt"] == 1)
		{
			$prefs['display']["Reply"] = true;
		}
		else
		{
			$prefs['display']["Reply"] = false;
		}
		
		if($temp["Everyone_Opt"] == 1)
		{
			$prefs['display']["Everyone"] = true;
		}
		else
		{
			$prefs['display']["Everyone"] = false;
		}
		
	
		// Friend Feed Prefrences
		if($temp["FFUser"] != 0)
		{
			$prefs['friendfeed']["Status"] = true;
			$prefs['friendfeed']["User"] = $temp["FFUser"];
			$prefs['friendfeed']["Key"] = $temp["FFKey"];
		}			
		else
		{
			$prefs['friendfeed']["Status"] = false;
		}
		return $prefs;
	}
	public function changeFilter($tempfeed)
	{
		if(isset($this->Login['login']['id']))
		{
			$Login = $this->Login;
			$prefs = $this->Login['prefs']['display'];
		}
		else
		{
			Global $Login;
			if(!isset($Login[id]))
			{
				echo "Error: Login Variable not Found";
				return false;
			}
		}
		$validfeeds = array( "Home" => 1,
							 "Reply" => 1,
							 "Everyone" => 1);
		if($validfeeds[$tempfeed] == 1)
		{
			$feed = $tempfeed;
		}
		if($prefs[$feed] == true)
		{
			$sql = "UPDATE prefrences SET %s_Opt = 0 WHERE User_ID = %d";
			$query = sprintf($sql, 
					mysql_real_escape_string($feed),
					$Login['login']['id']);
			$this->mySql->makeQuery($query);
		}
		elseif($prefs[$feed] == false)
		{
			$sql = "UPDATE prefrences SET %s_Opt = 1 WHERE User_ID = %d";
			$query = sprintf($sql, 
					mysql_real_escape_string($feed),
					$Login['login']['id']);
			$this->mySql->makeQuery($query);
		}
		return true;
	}
}
?>